package com.xcms.test.news;

import Homework.cms.util.Page;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class NewsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        if("add".equals(action)){
            String title=request.getParameter("title");
            News news=new News();
            news.setTitle(title);
            if(NewsDao.insert(news)){   //如果添加成功，更新content里面的LatestNews
                ServletContext context=this.getServletContext();
                List<News> newsList=NewsDao.getLatestNews();
                context.setAttribute("LatestNews",newsList);
            }
            response.sendRedirect("admin/newsmanager.jsp");
        }
        if("page".equals(action)){
            int currPage=Integer.parseInt(request.getParameter("currPage"));
            Page<News> page=NewsDao.getPage(10,currPage);
            request.setAttribute("page",page);
            request.getRequestDispatcher("about2.jsp").forward(request,response);
        }
//        if("page2".equals(action)){
//            int currPage=Integer.parseInt(request.getParameter("currPage"));
//            Page<News> page2=NewsDao.getPage(10,currPage);
//            request.setAttribute("page2",page2);
//            request.getRequestDispatcher("manage/design.jsp").forward(request,response);
//        }
    }
}
